<template>
  <el-row :gutter="40" class="panel-group">
    <el-col :lg="4" :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" style="border-radius: 10px 10px 10px 10px;">
        <div class="card-panel-description">
          <div class="card-panel-text">
            资产维修率
          </div>
          <div class="valueTitle">
            {{ this.data.repotRate ? (data.repotRate > 100 ? 100 : data.repotRate) : 0 }}%
          </div>
        </div>
      </div>
    </el-col>
    <el-col :lg="4" :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" style="border-radius: 10px 10px 10px 10px;">
        <div class="card-panel-description">
          <div class="card-panel-text">
            资产完好率
          </div>
          <div class="valueTitle">
            {{ this.data.intactDate ? (data.intactDate < 0 ? 0 : data.intactDate.toFixed(2)) : 0 }}%
          </div>
        </div>
      </div>
    </el-col>
    <el-col :lg="4" :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" style="border-radius: 10px 10px 10px 10px;">
        <div class="card-panel-description">
          <div class="card-panel-text">
            购置总成本
          </div>
          <count-to :decimals="2" :duration="3600" :end-val="data.bulMoney ? data.bulMoney : 0" :start-val="0"
                    class="card-panel-num"/>
        </div>
      </div>
    </el-col>
    <el-col :lg="4" :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" style="border-radius: 10px 10px 10px 10px;">
        <div class="card-panel-description">
          <div class="card-panel-text">
            维修总金额
          </div>
          <count-to :decimals="2" :duration="3600" :end-val="data.repairCountMoney ? data.repairCountMoney : 0"
                    :start-val="0"
                    class="card-panel-num"/>
        </div>
      </div>
    </el-col>
    <el-col :lg="4" :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" style="border-radius: 10px 10px 10px 10px;">
        <div class="card-panel-description">
          <div class="card-panel-text">
            折旧总金额
          </div>
          <count-to :decimals="2" :duration="3600" :end-val="data.depreciationMoney ? data.depreciationMoney : 0"
                    :start-val="0"
                    class="card-panel-num"/>
        </div>
      </div>
    </el-col>
    <el-col :lg="4" :sm="12" :xs="12" class="card-panel-col">
      <div class="card-panel" style="border-radius: 10px 10px 10px 10px;">
        <div class="card-panel-description">
          <div class="card-panel-text">
            增值总金额
          </div>
          <count-to :decimals="2" :duration="3600" :end-val="data.addedMoney ? data.addedMoney : 0" :start-val="0"
                    class="card-panel-num"/>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from 'vue-count-to'
import {assetHeadStatistics} from '@/api/cld/assets'

export default {
  components: {
    CountTo
  },
  props: {
    dateRange: {
      type: Array
    }
  },
  data() {
    return {
      data: {},
      queryParams: {}
    }
  },
  watch: {
    dateRange(val) {
      this.getInfo();
    }
  },
  created() {
    this.getInfo()
  },
  methods: {
    getInfo() {
      assetHeadStatistics(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        this.data = res.data;
        this.data.repotRate = this.calculatePercentages(this.data.repotNumber, this.data.assetsCount);
        this.data.intactDate = 100 - (this.data.repotRate ? this.data.repotRate : 0);
        this.data.repairCountMoney = this.data.repairMoney + (this.data.repairOtherMoney ? this.data.repairOtherMoney : 0);
      })
    },
    calculatePercentages(numerator, denominator) {
      if (denominator === 0 || isNaN(numerator) || isNaN(denominator)) {
        return "0"; // 防止除以0或非数字
      }
      const percentage = ((numerator / denominator) * 100).toFixed(2); // 保留两位小数
      return percentage; // 返回格式化的百分比字符串
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  .card-panel-col {
    margin-bottom: 10px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    border-color: rgba(0, 0, 0, .05);
    box-shadow: 5px 5px 5px #DCDFE6;
    border:1px #DCDFE6 solid;
    margin-bottom: 10px;

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      font-weight: bold;
      margin: 26px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}

.valueTitle {
  font-size: 18px;
}
</style>
